<script setup lang='ts'>
  import { reactive, ref } from 'vue';
  import { numberApi } from '@/api/controller';
  import { Toastloading, ToastSuccess, ToastWarning } from '@/utils/public';
  import Pagination from "@/components/Pagination/index.vue";
  import prohibitForm from './prohibitForm.vue';
  import { ElMessageBox } from 'element-plus';
  const prohibitFormRef = ref()
  const action = reactive({
    total:0,
    list:[] as any[],
    device:[] as any[],
  })
  const listQuery = reactive({
    page:1,
    limit:20,
    uuid:'',
  })
  const handleSerach = ()=>{
    listQuery.page = 1
    getList()
  }
  const handleForm = (type=1)=>{
    prohibitFormRef.value.openDialog({
      guid:action.device.length > 0? action.device[0].guid : '',
      type
    })
  }
  const handleResetting = async(row:any = {})=>{
    const { user_id=0 } = row
    if(!user_id) return ToastWarning('缺少用户user_id')
    ElMessageBox.confirm("确定重置该用户设备吗?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
    .then(async () => {
      const loading = Toastloading();
      await numberApi.resetDevice(user_id);
      loading.close();
      ToastSuccess("操作成功!");
      getList();
    })
    .catch(() => {});
  }
  const getList = async()=> {
    if(!listQuery.uuid) return ToastWarning('请输入用户UUID')
    const loading = Toastloading()
    const res = await numberApi.bannedEquipment(listQuery)
    loading.close()
    const { list={}, device=[]  } = res?.data || {}
    const { data=[], total=0 } = list || {}
    action.total = total
    action.list = data
    action.device = device
  }
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">用户UUID：</div>
          <el-input v-model="listQuery.uuid" placeholder="请输入UUID" />
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="danger" v-rule="'banneDequipment'" @click="handleForm(1)">封禁</el-button>
          <el-button type="primary" v-rule="'banneDequipment'" @click="handleForm(2)">解封</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-TableCenter xm-mb20"
      :data="action.list"
      border>
      <el-table-column width="120" prop="uuid" label="UUID" />
      <el-table-column min-width="120" show-overflow-tooltip prop="nickname" label="昵称" />
      <el-table-column width="200" label="操作" >
        <template #default="scoped">
          <el-button
            type="primary"
            @click="handleResetting(scoped.row)"
            v-rule="'banneDequipment'"
          >重置设备</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-table
      class="xm-TableCenter"
      :data="action.device"
      border>
      <el-table-column width="120" prop="version_no" label="APP版本号" />
      <el-table-column min-width="120" prop="os_name" label="设备系统" />
      <el-table-column min-width="120" prop="device" label="设备型号" />
      <el-table-column min-width="120" prop="created_at" label="创建时间" />
      <el-table-column width="180">
        <template #default="scoped">
          <span v-if="scoped.row.forbid_status == 2" style="color: blue"
            >正常</span
          >
          <span v-else-if="scoped.row.forbid_status == 1" style="color: red"
            >已封禁</span
          >
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <prohibitForm ref="prohibitFormRef" @update-list="getList" />
  </div>
</template>